<!--# layout("/layout/map.html",{}){  #-->
<style>
<!--
body, html, #allmap {
	width: 100%;
	height: 100%;
	overflow: hidden;
	margin: 0;
	font-family: "微软雅黑";
}
-->
</style>
<div id="search" class="row">
	<div class="col-xs-12 col-md-12 col-sm-12">
		<div class="input-group">
			<input class="form-control" type="text" v placeholder="请输入地址" id="autoComplete" name="autoComplete"> <span class="input-group-addon"> <i
				class="ace-icon fa fa-search"></i>
			</span>
		</div>
	</div>
	<div class="col-xs-4"></div>
</div>
<div id="allmap"></div>
<input type="hidden" id="data">
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=${obj.data.ak!}"></script>
<script type="text/javascript">
	var map;
	$(function() {
		map = new BMap.Map("allmap"); // 创建Map实例

		map.centerAndZoom(new BMap.Point(${obj.data.lng}, ${obj.data.lat}), 15); // 初始化地图,设置中心点坐标和地图级别

		new BMap.Geocoder().getLocation(new BMap.Point(${obj.data.lng}, ${obj.data.lat}), function(rs) {
			var marker = new BMap.Marker(rs.point);
			map.clearOverlays();
			map.addOverlay(marker); // 将标注添加到地图中

			$("#data").val(JSON.stringify(rs));
			layer.msg(rs.address + '<br> (' + rs.point.lng + ',' + rs.point.lat + ')', {
				icon : 6
			});
		});

		map.addControl(new BMap.MapTypeControl()); //添加地图类型控件

		map.enableScrollWheelZoom(true);
		map.addEventListener("click", function(e) {
			var pt = e.point;
			new BMap.Geocoder().getLocation(pt, function(rs) {
				var marker = new BMap.Marker(rs.point);
				map.clearOverlays();
				map.addOverlay(marker); // 将标注添加到地图中

				$("#data").val(JSON.stringify(rs));
				layer.msg(rs.address + '<br> (' + rs.point.lng + ',' + rs.point.lat + ')', {
					icon : 6
				});
			});
		});

		var ac = new BMap.Autocomplete({//建立一个自动完成的对象
			"input" : "autoComplete",
			"location" : map
		});

		ac.addEventListener("onhighlight", function(e) { //鼠标放在下拉列表上的事件
			var str = "";
			var _value = e.fromitem.value;
			var value = "";
			if (e.fromitem.index > -1) {
				value = _value.province + _value.city + _value.district + _value.street + _value.business;
			}
			str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
			value = "";
			if (e.toitem.index > -1) {
				_value = e.toitem.value;
				value = _value.province + _value.city + _value.district + _value.street + _value.business;
			}
			str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
			$("#searchResultPanel").html(str);
		});

		ac.addEventListener("onconfirm", function(e) { //鼠标点击下拉列表后的事件

			var _value = e.item.value;
			var myValue = _value.province + _value.city + _value.district + _value.street + _value.business;

			var local = new BMap.LocalSearch(map, { //智能搜索
				onSearchComplete : function() {
					var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
					new BMap.Geocoder().getLocation(pp, function(rs) {
						var marker = new BMap.Marker(rs.point);
						map.centerAndZoom(rs.point, 18);
						map.clearOverlays();
						map.addOverlay(marker); // 将标注添加到地图中

						$("#data").val(JSON.stringify(rs));
						layer.msg(rs.address + '<br> (' + rs.point.lng + ',' + rs.point.lat + ')', {
							icon : 6
						});
					});
				}
			});
			local.search(myValue);

			$("#searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
		});
	})

	function showMsg() {
		layer.msg("没有选择地点!", {
			icon : 5
		});
	}
</script>
<!--# } #-->


<script type="text/javascript">
	// 百度地图API功能
	function G(id) {
		return document.getElementById(id);
	}

	var map = new BMap.Map("l-map");
	map.centerAndZoom("北京", 12); // 初始化地图,设置城市和地图级别。

	var ac = new BMap.Autocomplete( //建立一个自动完成的对象
	{
		"input" : "suggestId",
		"location" : map
	});

	ac.addEventListener("onhighlight", function(e) { //鼠标放在下拉列表上的事件
		var str = "";
		var _value = e.fromitem.value;
		var value = "";
		if (e.fromitem.index > -1) {
			value = _value.province + _value.city + _value.district + _value.street + _value.business;
		}
		str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

		value = "";
		if (e.toitem.index > -1) {
			_value = e.toitem.value;
			value = _value.province + _value.city + _value.district + _value.street + _value.business;
		}
		str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
		G("searchResultPanel").innerHTML = str;
	});

	var myValue;
	ac.addEventListener("onconfirm", function(e) { //鼠标点击下拉列表后的事件
		var _value = e.item.value;
		myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
		G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

		setPlace();
	});

	function setPlace() {
		map.clearOverlays(); //清除地图上所有覆盖物
		function myFun() {
			var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
			map.centerAndZoom(pp, 18);
			map.addOverlay(new BMap.Marker(pp)); //添加标注
		}
		var local = new BMap.LocalSearch(map, { //智能搜索
			onSearchComplete : myFun
		});
		local.search(myValue);
	}
</script>